﻿//    Copyright 2010,2011 Himanshu Vasishth
//   
//    This file is part of SecretVault.
//
//    SecretVault is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    SecretVault is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with SecretVault.  If not, see <http://www.gnu.org/licenses/>.
//
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Windows.Forms;
using Common.Exceptions;
using System.IO;
using System.Diagnostics;
using PasswordLocker.Properties;
using Common;

namespace PasswordLocker
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {

            Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
            Application.ThreadException += ApplicationThreadException;
            AppDomain currentDomain = AppDomain.CurrentDomain;
            currentDomain.UnhandledException += UnhandledExceptionHandler;

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            // Initiailize the tracing
            if (Settings.Default.TracingEnabled)
            {
                Log.EnableTracing();
            }
            if (Settings.Default.ShowTraceLog)
            {
                Log.ShowTraceWindow();
            }
            Log.TraceLevel = Settings.Default.TraceLevel;

            // Create the PasswordManager to start the app
            new PasswordManager().StartApplication();
        }

        /// <summary>
        /// Called when an exception gets thrown by any thread which is not handled
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        static void UnhandledExceptionHandler(object sender, UnhandledExceptionEventArgs e)
        {
            ShowException(e.ExceptionObject);
        }

        /// <summary>
        /// Called when a thread owned by windows forms throws an unhandled exception
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        static void ApplicationThreadException(object sender,
            System.Threading.ThreadExceptionEventArgs e)
        {
            ShowException(e.Exception);
        }

        private static void ShowException(object ex)
        {
            // TODO: use a better ui
            string message = ex is PasswordLockerException
                                 ? ((Exception) ex).Message
                                 : string.Format(Resources.UnexpectedError, ex);
            MessageBox.Show(message,
                            Resources.AppName,
                            MessageBoxButtons.OK,
                            MessageBoxIcon.Error);
            Application.Exit();
        }
    }
}
